﻿using System;
using log4net;
using Mate.Caching;
using Mate.Core;
using Mate.Core.Base;
using Mate.Db.Model.Users;
using Mate.Dto.Users;
using Mate.Framework.BaseController.Admin;
using Mate.Framework.Extensions;
using Mate.System.Service;
using Mate.User.Service;
using Microsoft.AspNetCore.Mvc;

namespace Mate.Admin.Controllers
{
    public class AdminUserController : BaseController
    {
        private readonly IAdminUserService _adminUserService;
        private readonly IRolesService rolesService;
        private readonly ILog log;
        private readonly ICacheManager _cache;
        public AdminUserController(AdminUserService adminUserService, IRolesService rolesService, ICacheManager cache, ILog log)
        {
            this.log = log;
            this._adminUserService = adminUserService;
            this.rolesService = rolesService;
            this._cache = cache;
        }

        public ActionResult Index()
        {
            return View();
        }

        #region 获取列表
        [HttpPost]
        public IActionResult GetList(int pageIndex, int pageSize, string Key)
        {
            int recordCount = 0;
            var list = _adminUserService.GetList(pageIndex, pageSize, Key, ref recordCount);
            var result = new
            {
                rows = list,
                total = recordCount
            };
            return Json(result);
        }
        #endregion

        #region 删除
        [HttpPost]
        public JsonResult Remove(int id)
        {
            var result = new BaseResult();

            var model = _adminUserService.Get(x=>x.id==id);
            if (model == null)
            {
                result.ResultCode = ResultCode.Fail;
                result.Message = "参数错误";
                return Json(result);
            }

            if (model.user_no == "admin")
            //if (model.IsSuperAdmin == true || model.RoleId == Consts.SuperAdminRoleId)
            {
                result.ResultCode = ResultCode.Fail;
                result.Message = "超级管理员账号不能删除!";
                return Json(result);
            }

            var b = _adminUserService.Delete(x=>x.id==id);
            if (b > 0)
            {
                _cache.Remove(string.Format(MateCacheKey.CacheUserIdKey, id));
                result.ResultCode = ResultCode.Success;
                result.Message = "删除成功";
            }
            else
            {
                result.ResultCode = ResultCode.Fail;
                result.Message = "删除失败";
            }
            return Json(result);
        }
        #endregion

        #region 添加
        public ActionResult Create()
        {
            var model = new AdminUserModel() { is_enable = true};
            model.Roles = rolesService.GetRoleAll();
            return View(model);
        }

        [HttpPost]
        public JsonResult Create(admin_user model)
        {
            var result = new BaseResult();

            if (model.user_no == "admin")
            {
                result.ResultCode = ResultCode.Fail;
                result.Message = "用户名不能使用admin，请重新输入";
                return Json(result);
            }

            model.create_time = DateTime.Now;
            model.is_delete = false;
            model.password = EncryptUtil.MD5Encrypt16(model.password);
            var obj = _adminUserService.Insert(model);
            if (obj > 0)
            {
                result.ResultCode = ResultCode.Success;
                result.Message = "添加成功";
            }
            else
            {
                result.ResultCode = ResultCode.Fail;
                result.Message = "添加失败";
            }
            return Json(result);
        }
        #endregion

        #region 修改
        public ActionResult Edit(int id)
        {
            var model = _adminUserService.Get(x => x.id == id);
            if (model == null)
            {
                return Content("参数错误");
            }
            ViewBag.Roles = rolesService.GetRoleAll();
            return View(model);
        }

        [HttpPost]
        public JsonResult Edit(admin_user model)
        {
            var result = new BaseResult();
            if (model == null)
            {
                result.ResultCode = ResultCode.Fail;
                result.Message = "参数错误";
                return Json(result);
            }
            
            var b = _adminUserService.Update(model, 
                x => new { x.user_name, x.sex, x.phone, x.roles_id, x.is_enable },
                x => new object[] { x.id});
            if (b > 0)
            {
                _cache.Remove(string.Format(MateCacheKey.CacheUserIdKey, model.id));
                result.ResultCode = ResultCode.Success;
                result.Message = "修改成功";
            }
            else
            {
                result.ResultCode = ResultCode.Fail;
                result.Message = "修改失败";
            }
            return Json(result);

        }
        #endregion

        //#region 修改密码
        //[NonAuthorization]
        //public ActionResult UpdatePwd()
        //{
        //    return View(new UpdatePwdModel());
        //}

        //[HttpPost]
        //[NonAuthorization]
        //public JsonResult UpdatePwd(UpdatePwdModel model)
        //{
        //    if (model.NewPwd1 != model.NewPwd2)
        //    {
        //        return Json(new BaseResult(false, "", "二次密码输入不一致！"));
        //    }

        //    if (model.OldPwd != CurrentAdminUser.Password)
        //    {
        //        return Json(new BaseResult(false, "", "修改密码失败，原密码错误！"));
        //    }

        //    if (_service.Updateable<AdminUser>(new AdminUser()
        //    {
        //        Password = model.NewPwd1,
        //        Id = CurrentAdminUser.Id
        //    }).ExecuteCommand() > 0)
        //    {
        //        _cache.Remove(string.Format(GjjCacheKey.CACHE_USER_ID_KEY, CurrentAdminUser.Id.ToString()));
        //        _log.Info(_MenuName, LogAction.修改密码, string.Empty, true);
        //        return Json(new BaseResult(true, "", "修改密码成功！"));
        //    }
        //    else
        //    {
        //        _log.Info(_MenuName, LogAction.修改密码, string.Empty, false);
        //        return Json(new BaseResult(false, "", "修改密码失败！"));
        //    }
        //}
        //#endregion



    }
}